package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.data.Result;
import com.dongdongshop.em.ResultEnum;
import com.dongdongshop.page.PageBeen;
import com.dongdongshop.pojo.TbBrand;
import com.dongdongshop.pojo.TbSpecification;
import com.dongdongshop.pojo.TbTypeTemplate;
import com.dongdongshop.service.TbBrandService;
import com.dongdongshop.service.TbTypeTemplateService;
import org.attoparser.dom.INestableNode;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("template")
public class TemplateController {
    @Reference
    private TbTypeTemplateService tbTypeTemplateService;

    @RequestMapping("jeep")
    public String jeep(Model model){
        List<TbBrand> brandList = tbTypeTemplateService.selectByBrand();
        List<TbSpecification> specificationList = tbTypeTemplateService.selectBySpecification();
        model.addAttribute("brandList",brandList);
        model.addAttribute("specList",specificationList);
        return "admin/type_template";
    }

    @RequestMapping("getTemplate")
    @ResponseBody
    public Result getTemplate(String name,
                              @RequestParam(defaultValue = "1")Integer pageNum,
                              @RequestParam(defaultValue = "4")Integer pageSize){
        PageBeen<TbTypeTemplate> pageBeen = tbTypeTemplateService.getTemplate(name,pageNum,pageSize);
        return Result.response(ResultEnum.SUCCESS).setData(pageBeen);
    }
    @RequestMapping("`addTemplate`")
    @ResponseBody
    public Result addTemplate(TbTypeTemplate tbTypeTemplate){
        int i = tbTypeTemplateService.addTemplate(tbTypeTemplate);
        return Result.response(ResultEnum.SUCCESS);
    }
    @RequestMapping("toUpdate")
    @ResponseBody
    public Result updateInfo(Long id,Model model){
        TbTypeTemplate tbTypeTemplate = tbTypeTemplateService.toUpdate(id);
        List<TbBrand> brandList = tbTypeTemplateService.selectByBrand();
        List<TbSpecification> specificationList = tbTypeTemplateService.selectBySpecification();
        model.addAttribute("brandList",brandList);
        model.addAttribute("specList",specificationList);
        return Result.response(ResultEnum.SUCCESS).setData(tbTypeTemplate);
    }
    @RequestMapping("updateInfo")
    @ResponseBody
    public Result updateInfo(TbTypeTemplate tbTypeTemplate){
        int i = tbTypeTemplateService.updateInfo(tbTypeTemplate);
        return Result.response(ResultEnum.SUCCESS);
    }
    @RequestMapping("deleteInfo/{ids}")
    @ResponseBody
    public Result deleteInfo (@PathVariable("ids")Integer[] ids){
        int i = tbTypeTemplateService.deleteInfo(ids);
        return Result.response(ResultEnum.SUCCESS);
    }



}
